package com.javasm.commons;

import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import javax.security.auth.Subject;
import java.util.Collection;

public class PhoneCaptchaAuthenticationToken extends AbstractAuthenticationToken {

    private Object phone;
    private Object captcha;


    public PhoneCaptchaAuthenticationToken(Object phone,Object captcha) {
        super(null);
        this.phone=phone;
        this.captcha=captcha;
        this.setAuthenticated(false);
    }
    public PhoneCaptchaAuthenticationToken(Object phone,Object captcha,Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.phone=phone;
        this.captcha=captcha;
        super.setAuthenticated(true);
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException("Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        } else {
            super.setAuthenticated(false);
        }
    }

    @Override
    public Object getCredentials() {
        return this.captcha;
    }

    @Override
    public Object getPrincipal() {
        return this.phone;
    }
}
